package com.changyi.device.printer;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.hisense.pos.spiprinter.SpiPrinter;
import com.landicorp.android.eptapi.device.Printer;
import com.landicorp.android.eptapi.utils.QrCode;
import com.sunmi.peripheral.printer.SunmiPrinterService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xin on 2021/3/29
 * desc:
 */
public class HisenseHi98Printer implements PrinterInterface {

    private Context mContext;
    private static HisenseHi98Printer mHi98Printer;
    private SDKAPI sdk = null;

    private HisenseHi98Printer(Context context) {
        mContext = context;
    }

    public static HisenseHi98Printer getInstance(Context context) {
        if (mHi98Printer == null) {
            synchronized (LandiA8Printer.class){
                if (mHi98Printer == null) {
                    mHi98Printer = new HisenseHi98Printer(context);
                }
            }
        }
        return mHi98Printer;
    }


    @Override
    public PrinterInterface initDevice() {
        sdk = SDKAPI.getInstance();
        int iret = sdk.initPrinter();
        if(iret == 0){
            Log.e("TAG","初始化成功!");
        }else{
            Log.e("TAG","初始化失败!");
        }
        iret = sdk.setPrnGray(3);
        if(iret == 0){
            Log.e("TAG","设置灰度为3!");
        }else{
            Log.e("TAG","设置灰度失败!");
        }
        return mHi98Printer;
    }

    @Override
    public PrinterInterface printTitle(String title) {
        sdk.setPrnText(title+"\n",2,0);
        return mHi98Printer;
    }

    @Override
    public PrinterInterface printText(String text) {
        sdk.setPrnText(text+"\n",1,0);
        return mHi98Printer;
    }

    @Override
    public PrinterInterface printList(List<String> mList) {
        List<String> codes=new ArrayList<>();
        for (String content : mList) {
            if(content.contains("|")){
                String[] aa = content.split("\\|");
                for (int i = 0 ; i <aa.length ; i++ ) {
                    sdk.setPrnText(aa[i]+"\n", 1, 0);
                }
            }else  if (content.contains("@@@")){
                codes.add(content);

            } else  if (content.contains("&&&")){
                codes.add(content);
            } else if(content.equals("  ")){
                codes.add(content);
            }
            else{
                sdk.setPrnText(content.trim()+"\n", 1, 0);
            }

        }
        int iret = sdk.startPrint();
        Toast.makeText(mContext, getErrorDescription(iret), Toast.LENGTH_SHORT).show();
        sdk=null;
        if(codes!=null&&codes.size()>0) {
            printBitmaps(codes);
        }
        return mHi98Printer;
    }

    @Override
    public PrinterInterface printCode(String code) {
        sdk.setPrnBitmap(ZXingUtils.creatBarcode(mContext,code,200,60,true), 0);
        return mHi98Printer;
    }

    @Override
    public PrinterInterface printQrCode(String qRCode) {
        sdk.setPrnBitmap(ZXingUtils.encodeAsBitmap(qRCode, BarcodeFormat.QR_CODE,100,180), 1);
        return mHi98Printer;
    }

    @Override
    public PrinterInterface feedLine(int feelLine) {
        sdk.feedPaper(feelLine, 0);
        return mHi98Printer;
    }

    public PrinterInterface printBitmaps(List<String> codes) {
        initDevice();
        for (String content : codes) {
            if (content.contains("@@@")) {
                String temp = content.substring(3, content.length());
                sdk.setPrnBitmap(ZXingUtils.encodeAsBitmap(temp, BarcodeFormat.QR_CODE, 200, 200), 1);

            } else if (content.contains("&&&")) {
                String temp = content.substring(3, content.length());
                sdk.setPrnBitmap(ZXingUtils.creatBarcode(mContext, temp, 300, 60, false), 0);
            } else{
                sdk.setPrnText(content.trim()+"\n", 1, 0);
            }
        }
        sdk.feedPaper(10,0);
        sdk.startPrint();
        return mHi98Printer;
    }


    private String getErrorDescription(int code) {
        switch (code) {
            case 0:
                return "打印成功!";
            case -52:
                return "打印机过热!";
            case -51:
                return "打印机缺纸!";
        }
        return "打印其他错误!";
    }
}
